package org.jeecg.modules.lab.utensil.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.lab.device.entity.LabDevice;
import org.jeecg.modules.lab.device.mapper.LabDeviceMapper;
import org.jeecg.modules.lab.utensil.entity.LabUtensil;
import org.jeecg.modules.lab.utensil.entity.LabUtensilChecklist;
import org.jeecg.modules.lab.utensil.entity.LabUtensilChecklistTo;
import org.jeecg.modules.lab.utensil.entity.MonthList;
import org.jeecg.modules.lab.utensil.mapper.LabUtensilChecklistMapper;
import org.jeecg.modules.lab.utensil.mapper.LabUtensilMapper;
import org.jeecg.modules.lab.utensil.service.ILabUtensilChecklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.MarshalledObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 器具检测详情清单
 * @Author: jeecg-boot
 * @Date:   2023-08-22
 * @Version: V1.0
 */
@Service
public class LabUtensilChecklistServiceImpl extends ServiceImpl<LabUtensilChecklistMapper, LabUtensilChecklist> implements ILabUtensilChecklistService {
	
	@Autowired
	private LabUtensilChecklistMapper labUtensilChecklistMapper;

	@Autowired
	private LabUtensilMapper labUtensilMapper;

	@Autowired
	private LabDeviceMapper labDeviceMapper;
	
	@Override
	public List<LabUtensilChecklist> selectByMainId(String mainId) {
		return labUtensilChecklistMapper.selectByMainId(mainId);
	}

	@Override
	public List<LabUtensilChecklistTo> queryBy(String uteRef){
		LambdaQueryWrapper<LabUtensilChecklist> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(LabUtensilChecklist::getUteRef,uteRef);
		return labUtensilChecklistMapper.queryBy(uteRef);
	};

	/**
	 * 提交校验信息
	 * @param labUtensilChecklist
	 */
    @Override
	@Transactional(rollbackFor = Exception.class)
	public void updateAll(LabUtensilChecklist labUtensilChecklist){
		LabUtensil labUtensil = labUtensilMapper.selectOne(new LambdaQueryWrapper<LabUtensil>().eq(LabUtensil::getId, labUtensilChecklist.getUteId()));
		labUtensil.setLastFirm(labUtensilChecklist.getLastFirm());//溯源单位
		labUtensil.setLastInspectDate(labUtensilChecklist.getLastDate());//上次检测
		labUtensil.setExpirationDate(labUtensilChecklist.getIndate());//有效期
		labUtensil.setCertificate(labUtensilChecklist.getCertificate());//检测证书报告
		labUtensil.setSynFlag("1");
		labUtensilChecklist.setSynFlag("1");
		labUtensilChecklistMapper.updateById(labUtensilChecklist);

		//同时根据器具id要修改基础表中的内容
		labUtensilMapper.updateById(labUtensil);
		//根据器具id查看是否有设备id，有的话修改设备信息
		if (labUtensil.getDeviceId()!=null&&!labUtensil.getDeviceId().isEmpty()){
			//有设备id，修改设备信息
			LabDevice labDevice = new LabDevice();
			labDevice.setId(labUtensil.getDeviceId());
			labDevice.setLastCheckDate(labUtensilChecklist.getLastDate());
			labDevice.setSynFlag("1");
			labDeviceMapper.updateById(labDevice);
		}
	}

	//按照月份统计器具检测数量
	@Override
	public Map<String, List> queryMonth(String selYear) {
		List<String> listMonth = new ArrayList<>();
		List<String> listValue = new ArrayList<>();
		Map<String, List> map = new HashMap<>();
		List<MonthList> monthLists = labUtensilChecklistMapper.queryMonth(selYear);
		monthLists.forEach(e->{
			listMonth.add(e.getMonth()+"月");
			listValue.add(e.getCount());
		});
        map.put("xAxis",listMonth);
		map.put("yAxis",listValue);
		return map;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void submit(LabUtensilChecklist labUtensilChecklist) {
		//修改清单中该器具的状态为已提交
		labUtensilChecklist.setSynFlag("1");
		labUtensilChecklistMapper.updateById(labUtensilChecklist);
		//回写修改基础表中该器具的状态为已提交
		LabUtensil labUtensil = labUtensilMapper.selectOne(new LambdaQueryWrapper<LabUtensil>().eq(LabUtensil::getId, labUtensilChecklist.getUteId()));
		labUtensil.setSynFlag("1");
		labUtensil.setInspDecide("已提交");
		labUtensilMapper.updateById(labUtensil);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateList(List<LabUtensilChecklist> list) {

		for (int i = 0; i < list.size(); i++) {
			LabUtensilChecklist checklist = list.get(i);
			checklist.setSynFlag("1");
			labUtensilChecklistMapper.updateById(checklist);
			LabUtensil utensil = labUtensilMapper.selectOne(new LambdaQueryWrapper<LabUtensil>().eq(LabUtensil::getId, checklist.getUteId()));
			utensil.setLastInspectDate(checklist.getLastDate());
			utensil.setExpirationDate(checklist.getIndate());
			utensil.setCertificate(checklist.getCertificate());
			utensil.setLastFirm(checklist.getLastFirm());
			utensil.setSynFlag("1");
            labUtensilMapper.updateById(utensil);
		}
	}


}
